<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>emit传随机数给父组件</title>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application">
        <child @add="add"></child>
        <p> Count:{{count}}</p>
    </div>
    <script>
        const App = Vue.createApp({
            setup() {
                const count = Vue.ref(0)
                const add = (i) => {
                    count.value += i
                    //接收传来的值
                }


                return {
                    count,
                    add
                }
            },
        });

        //子组件
        const childBlock = {
            setup(props, ctx) {
                const clickBtn = () => {
                    ctx.emit("add", Math.random())
                    //自定义触发事件，传出值
                }
                return {
                    clickBtn
                }

            },

            template: ' <button type="button" @click="clickBtn">生成一个随机数</button>'
        }
        App.component("child", childBlock);

        //挂载
        App.mount("#Application");
    </script>
</body>

</html>